package com.shoubei.cashiedesk.base;

import android.app.ActivityGroup;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.res.Configuration;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;
import com.shoubei.cashiedesk.utils.MVersionConfig;

@SuppressWarnings("deprecation")
public class BasisActivity extends ActivityGroup {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// 设置一下版本，如果内存被清除国际化设置就会清除，这里会重新调用，一定要写在setContentView之前，因为里面一些国际化资源文件调用，这里添加设置语言
		// 为了解决有时候崩溃问题，不知道怎么出现的
		MVersionConfig.updateMVersion();
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		super.onCreate(savedInstanceState);
		// CashierDeskApp.getInstance().addActivity(this);
		// if (CashierDeskApp.cdApp.with == 0) {
		WindowManager windowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
		DisplayMetrics metrics = new DisplayMetrics();// 1920*1008(1.5)
														// 1024*720(1.0)
														// 1920*1032(1.0)
														// 2048*1536(2.0)
														// 2560*1600(2.0)
		windowManager.getDefaultDisplay().getMetrics(metrics);
		App.getInstance().width = metrics.widthPixels;
		App.getInstance().height = metrics.heightPixels;
		App.getInstance().screenDensity = metrics.densityDpi;
//		L.e("屏幕宽：" + App.getInstance(). + "；屏幕高：" + CashierDeskApp.cdApp.height + "；屏幕密度：" + metrics.densityDpi);
//		L.e("onCreate:" + getClass().getName());

	}

	@Override
	public void unregisterReceiver(BroadcastReceiver receiver) {
		try {
			super.unregisterReceiver(receiver);
		} catch (Exception e) {
		}
		receiver=null;
	}

	@Override
	public boolean dispatchTouchEvent(MotionEvent ev) {
		try {
			if (ev.getAction() == MotionEvent.ACTION_DOWN) {
				View v = getCurrentFocus();
				if (isShouldHideInput(v, ev)) {
					InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
					if (imm != null) {
						imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return super.dispatchTouchEvent(ev);
	}


	public boolean isShouldHideInput(View v, MotionEvent event) {
		if (v != null && (v instanceof EditText)) {
			int[] leftTop = { 0, 0 };
			// 获取输入框当前的location位置
			v.getLocationInWindow(leftTop);
			int left = leftTop[0];
			int top = leftTop[1];
			int bottom = top + v.getHeight();
			int right = left + v.getWidth();
			if (event.getX() > left && event.getX() < right && event.getY() > top && event.getY() < bottom) {
				// 点击的是输入框区域，保留点击EditText的事件
				return false;
			} else {
				return true;
			}
		}
		return false;
	}

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
	}

	@Override
	public void onLowMemory() {
		super.onLowMemory();
	}

	@Override
	protected void onResume() {
		super.onResume();
		// MobclickAgent.onPageStart("SplashScreen"); // 统计页面
		MVersionConfig.updateMVersion();

	}

	@Override
	protected void onPause() {
		super.onPause();
		// MobclickAgent.onPageEnd("SplashScreen"); // 保证 onPageEnd 在onPause
		MVersionConfig.updateMVersion();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

	public boolean isNull(TextView text) {
		if (text.getText().toString().equals("")) {
			return true;
		}
		return false;
	}

	public boolean isNull(String text) {
		if (TextUtils.isEmpty(text)) {
			return true;
		}
		return false;
	}

	public boolean isNull(EditText text) {
		if (text.getText().toString().equals("")) {
			return true;
		}
		return false;
	}

	public String getText(EditText text) {
		return text.getText().toString();
	}

	public String getText(TextView text) {
		return text.getText().toString();
	}

}
